package cn.fzc.yipai.service.impl;

import cn.fzc.yipai.entity.*;
import cn.fzc.yipai.mapper.GoodsListMapper;
import cn.fzc.yipai.service.GoodsListService;
import cn.fzc.yipai.utils.HttpUtils;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import lombok.SneakyThrows;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lyh
 * @since 2021-07-20
 */
@Service
public class GoodsListServiceImpl extends ServiceImpl<GoodsListMapper, GoodsList> implements GoodsListService {

    /**
     * 获取活动场次
     */
    public static final String GOODS_SELLER_LIST_URL = "https://www.yihui-xh.xyz/yip/otc/advertise/getgoodssellerlist/%s/1/-1/1/500";

    public static final String TOKEN = "2c96d49bac29a4578c2b91b8ac6bff3870";


    @Autowired
    private HttpUtils httpUtils;


    @SneakyThrows
    @Override
    public List<NewGoodsInfo> getLocalGoodsList() {

        String fileToString = FileUtils.readFileToString(new File("C:\\Users\\admin\\Desktop\\tmp\\pai.json"));

        List<NewGoodsInfo> newGoodsInfos = JSON.parseArray(fileToString, NewGoodsInfo.class);

        return newGoodsInfos;
    }

    @Override
    public List<NewGoodsInfo> getDbGoodsList(Integer storeId, String time) {
        QueryWrapper<GoodsList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id", storeId);
        queryWrapper.eq("create_time", time);
        List<GoodsList> list = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.stream().map(goodsList -> NewGoodsInfo.builder()
                    .goodsInfoVO(JSON.parseObject(goodsList.getGoodsInfoVo(), GoodsInfo.class))
                    .goodsSellerVO(JSON.parseObject(goodsList.getGoodsInfoVo(), GoodsSeller.class))
                    .build()).collect(Collectors.toList());
        }
        return Lists.newArrayList();
    }

    @Override
    public void updateGoodsList() {
        for (int i = 1; i < 4; i++) {
            String result = httpUtils.executeGet(String.format(GOODS_SELLER_LIST_URL, i), TOKEN);
            if (StringUtils.isNotBlank(result)) {
                Response<PageInfo<NewGoodsInfo>> pageInfoResponse = JSON.parseObject(result, new TypeReference<Response<PageInfo<NewGoodsInfo>>>() {
                });
                List<NewGoodsInfo> list = pageInfoResponse.getResult().getList();
                List<GoodsList> goodsLists = new ArrayList<>();
                for (NewGoodsInfo newGoodsInfo : list) {
                    GoodsList goodsList = new GoodsList();
                    goodsList.setStoreId(i);
                    goodsList.setGoodsId(newGoodsInfo.getGoodsInfoVO().getId());
                    goodsList.setGoodsInfoVo(JSON.toJSONString(newGoodsInfo.getGoodsInfoVO()));
                    goodsList.setGoodsSellerVo(JSON.toJSONString(newGoodsInfo.getGoodsSellerVO()));
                    goodsList.setStatus(1);
                    goodsList.setCreateTime(DateUtil.date());
                    goodsList.setUpdateTime(DateUtil.date());
                    goodsList.setPhone(newGoodsInfo.getGoodsSellerVO().getMName());
                    goodsList.setBasePrice(newGoodsInfo.getGoodsInfoVO().getBasePrice());
                    goodsList.setQuotePrice(newGoodsInfo.getGoodsInfoVO().getQuotePrice());
                    goodsLists.add(goodsList);
                }
                if (CollectionUtils.isNotEmpty(goodsLists)) {
                    for (GoodsList goodsList : goodsLists) {
                        try {
                            this.save(goodsList);
                        } catch (Exception e) {
                        }
                    }
                }
            }
        }
    }

    @Override
    public void inertOrUpdate(GoodsList g) {
        baseMapper.inertOrUpdate(g);
    }
}
